package com.example.littleken.ormlitetext;

import android.test.InstrumentationTestCase;
import android.util.Log;

import com.example.littleken.ormlitetext.bean.School;
import com.example.littleken.ormlitetext.bean.Student;
import com.example.littleken.ormlitetext.db.DatabaseHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.misc.TransactionManager;
import com.j256.ormlite.stmt.UpdateBuilder;

import java.sql.SQLException;
import java.util.List;
import java.util.concurrent.Callable;

/**本类用于单元测试
 * Created by littleken on 2018/5/9.
 */
//继承自单元测试类
public class OrmliteText extends InstrumentationTestCase{

    public DatabaseHelper getHelper(){
        return DatabaseHelper.getLnstance(this.getInstrumentation().getTargetContext());
    }

    public Dao<School,Integer> getSchoolDao() throws SQLException {
        return getHelper().getDao(School.class);
    }

    public Dao<Student,Integer> getStudentDao() throws SQLException {
        return getHelper().getDao(Student.class);
    }

    public void testInsert() throws SQLException {
        Dao<Student,Integer> stuDao  = getStudentDao();
        Dao<School,Integer> schDao = getSchoolDao();
        School school = new School("北京理工大学","北京");
        schDao.create(school);

        Student stu1 = new Student("测试1",21,"66668888",school);
        Student stu2 = new Student("测试2",22,"66662333",school);
        Student stu3 = new Student("测试3",23,"23336666",school);
        stuDao.create(stu1);
        stuDao.create(stu2);
        stuDao.create(stu3);
    }

    public void testQuery() throws SQLException {
        Dao<Student,Integer> stuDao = getStudentDao();
        List<Student> students = stuDao.queryForAll();

        for (Student stu : students){
            Log.e("TAG",stu.toString()+"school:"+stu.getSchool());
        }

        Dao<School,Integer> schDao = getSchoolDao();
        List<School> schools =schDao.queryForAll();
        for(School sch : schools){
            Log.e("TAG",sch.toString());
            for(Student stu : sch.getStudents()){
                Log.e("TAG","student:"+stu.toString());
            }
        }
       /* Student stu1 = stuDao.queryForId(3);
        //Log.e("TAG",stu1.toString());
        List<Student> stu2 = stuDao.queryForEq("name","测试2");
        for (Student stu: stu2){
            Log.e("TAG",stu.toString());
        }*/
    }

    public void testUpdate() throws SQLException {
        Dao<Student,Integer> stuDao = getStudentDao();
        //通过更新构建器来构建更新语句
        UpdateBuilder update = stuDao.updateBuilder();
        update.setWhere(update.where().eq("id",1));
        //更新后的值
        update.updateColumnValue("name","测试更新");
        update.updateColumnValue("phone",110);
        //更新
        update.update();
    }

    public void testDelete() throws SQLException {
        Dao<Student,Integer> stuDao = getStudentDao();
        stuDao.deleteById(1);
    }

    public void testTransaction() throws SQLException {
        final Dao<Student,Integer> stuDao = getStudentDao();
        final Student stu = new Student("测试事务学生",20,"110",new School("测试事务学校","学校地址"));

        TransactionManager.callInTransaction(getHelper().getConnectionSource(),
                new Callable<Void>() {
                    @Override
                    public Void call() throws Exception {
                        for (int i = 0; i <20 ; i++) {
                            stuDao.create(stu);
/*                            if(i == 10){
                                throw new SQLException("test...");
                            }*/
                        }
                        return null;
                    }
                }
        );
    }
}
